package edu.towson.cosc.cosc455.interfacesOIMANI1markdowncompiler;
import java.util.*;
import java.io.*;

public class MyCompilerClass {

	// Global variable for the current token
	public static String currentToken;
	// Instance of the lexical analyzer
	public static MyLexicalAnalyzer Lexer;
	public static MySyntaxAnalyzer Parser;
	public static MySemanticAnalyzer Semance;
	static Queue<String> parseTres = new LinkedList<String>();
	static Queue<String> endingParseTree = new LinkedList<String>();
	
	public static void main(String[] args) throws IOException, CompilerException {
		// check to make sure correct arguments
		// check to make sure the file has an mkd extension (ends with .mkd)
	    Scanner console = new Scanner(System.in);
		System.out.println("Please Enter the .mkd extention File you would like to compile: ");
	    String mkdFile = console.next();
	    String extension = mkdFile.substring(mkdFile.lastIndexOf('.')+1);
		if(!extension.equals("mkd")){
			System.out.println("Sorry, the file name you entered does not have a .MKD extension. Please try again");	
		}
		else{
		// initializers
		Lexer = new MyLexicalAnalyzer();
		Parser = new MySyntaxAnalyzer();
		Semance = new MySemanticAnalyzer();
		
		// call the lexical analyzer to get the first token set
		Lexer.MyLexicalAnalyzerStarter(mkdFile);
		// Get the first token
		Lexer.getNextToken();
		
		// call the state of the parser, i.e, document()
		Parser.document();
		parseTres = Parser.getQueue();
		//6. call the semantic analyzer
		endingParseTree = Semance.semanticAnalyzer(parseTres);
		
		// write the output file to html
		File file = new File("Test1.html");
	    BufferedWriter out = new BufferedWriter(new FileWriter(file));
	    try {
	    	String inputLine;
	    	int i =0;
	    	while(!endingParseTree.isEmpty()){
	        inputLine = endingParseTree.remove();
	        out.write(inputLine);
	        }
	        System.out.print("Write Successful");
	    } catch(IOException e1) {
	        System.out.println("Error during reading/writing");
	    } finally {
	    	//Close the file and clean up
	        out.close();
	      }
		}
	}

}
